package com.example.blog.controller;

import com.example.blog.service.FileService;
import com.example.blog.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: lu
 * @Date: 2023-11-23-17:13
 * @Description: 文件上传、下载
 */
@RestController
@RequestMapping(value = "/file")
@Slf4j
@Api(tags = "文件上传、下载")
public class FileController {
    @Autowired
    private FileService fileService;

    /**
     * @param multipartFile
     * @return 单个文件上传
     */
    @ApiOperation(value = "单文件上传")
    @PostMapping("/upload")
    public JsonResult<Object> uploadFile(@RequestParam("uploadFile") MultipartFile multipartFile) {
        return fileService.uploadFile(multipartFile);
    }

    /**
     * 文件下载
     * @param name
     * @param response
     */
    @ApiOperation("文件下载")
    @GetMapping("/download/{name}")
    public void download(@PathVariable String name, HttpServletResponse response) {
        try {
            fileService.downloadFile(name, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("测试")
    @GetMapping("/test")
    public  String test1(@RequestParam String name, @RequestParam String password){
        System.out.println("name:" + name +"," + "password: " + password);
        return "success";
    }


    /**
     * 批量上传文件接口
     * @param request
     * @return
     */
    @ApiOperation(value = "批量上传文件")
    @PostMapping("/uploads")
    public JsonResult<Object> uploadFiles(HttpServletRequest request){
        return fileService.uploads(request);
    }


}
